"use strict";
var _lessonid=null;
var _phase=null;
/////////Global  var/////////////////
$(function(){
var param=getParamsFromUrl();
_lessonid=param[0];
_phase=param[1];
if(_phase==0){
    $("#applyLessonBtn").hide();
}
getLessonHttp(_lessonid,_phase);
});
function getParamsFromUrl(){
    var     url = decodeURI(location.href);
    var  urlParmListStr = url.substr(url.indexOf('?') + 1, url.length);
    var  params=urlParmListStr.split("&");
var  param1=params[0].split("=")[1];
var  param2=params[1].split("=")[1];
    return [param1,param2];
}


function confirmLessonApply(){
	 layer.confirm(langThis("ol_apply"),{btn:[
        langThis("pm_confirm"),
        langThis("pm_cancle")
    ], title:langThis("lab_atten")},function(){
        layer.closeAll();     
    },function(){layer.closeAll();});
}
function fillLseeonDetail(theLesson){
$("#lessonTitle").html(theLesson.lessonname);
$("#lessonContent").html(theLesson.description);
$("#renshu").append(theLesson.currentnb);
$("#pici").append(theLesson.phase);
$("#lessonImg").attr('src',theLesson.image);
$("#teacherImg").attr('src',theLesson.ownerphoto);
$("#teacherName").html(theLesson.ownername);
$("#teacherInfo").html(theLesson.ownerdes);
var labs=theLesson.labs;
var labsHtml="";
for (var i in  labs){
    var thelab=labs[i];
labsHtml+="<li class=\"media\"><h4>"+thelab.name+"</h4><p>"+thelab.description+"</p></li>";
}
$("#lessonLabs").html(labsHtml);
}
function getLessonHttp(id,phase){

    var _url;
    if(phase>0){
    _url ="publishlessons/"+id+"/"+phase+"/lessoninfo";}
    else{
        _url ="lessons/"+id+"/lessoninfo";
    }
      $.ajax({
        url:_url,
        type:"GET",
        data:{},
        dataType:"json",
        success:function(data){
            if(data!=undefined&&data!=null) {
                
               fillLseeonDetail(data);

              
            }
            else {
              
            }
           
        },
        error:function(event){
           
           
        }
    });

}
function applyLesson(e){
    if(UserID=='null'||UserID==null){
         layer.msg(langThis("rc_loginfirst"));
         return;
    }
	var url="publishlessons/"+_lessonid+"/"+_phase+"/studentlist";
	var param= {"userinfoid":UserID};
	$.ajax({
		url:  url,
		type: "POST",
		data:JSON.stringify(param),
		dataType: "json",
		contentType:"application/json",
		success: function (data){
                if(data==true) {
                //success
               layer.msg(langThis("kc_appLessonSuc"));

 				e.disabled=true;
            }
            else {
                layer.msg(langThis("kc_appLessonFail"));
            }

        },
		error: function (event) {
           layer.msg(langThis("kc_appLessonFail"));
		
		}
	});
}